
package com.ebrg.secure9.activity;

import java.util.ArrayList;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.ebrg.secure9.Account;
import com.ebrg.secure9.AccountStats;
import com.ebrg.secure9.BaseAccount;
import com.ebrg.secure9.Secure9;
import com.ebrg.secure9.Preferences;
import com.ebrg.secure9.SearchAccount;

import com.ebrg.secure9.activity.K9ListActivity;
import com.ebrg.secure9.activity.Accounts.AccountsAdapter;

import com.ebrg.secure9.activity.setup.AccountSetupBasics;

import com.ebrg.secure9.controller.MessagingController;
import com.ebrg.secure9.controller.MessagingListener;

import com.ebrg.secure9.R;


public class EncryptionBasics extends K9ListActivity implements OnItemClickListener, OnClickListener
{

private ConcurrentHashMap<String, AccountStats> accountStats = new ConcurrentHashMap<String, AccountStats>();
ActivityListener mListener = new ActivityListener();

private ConcurrentHashMap<BaseAccount, String> pendingWork = new ConcurrentHashMap<BaseAccount, String>();



private AccountsAdapter mAdapter;
private SearchAccount unreadAccount = null;
private SearchAccount integratedInboxAccount = null;


	//Secure9
	private static String ACCOUNT_STATS = "accountStats";
	private static String SELECTED_CONTEXT_ACCOUNT = "selectedContextAccount";

	public static final String EXTRA_STARTUP = "startup";


	public static void actionLaunch(Context context)
	{
		Intent intent = new Intent(context, EncryptionBasics.class);
		intent.putExtra(EXTRA_STARTUP, true);
		context.startActivity(intent);
	}



	@Override
	public void onCreate(Bundle icicle)
	{

		
		unreadAccount = new SearchAccount(this, false, null, null);
		unreadAccount.setDescription(getString(R.string.search_all_messages_title));
		unreadAccount.setEmail(getString(R.string.search_all_messages_detail));

		integratedInboxAccount = new SearchAccount(this, true, null,  null);
		integratedInboxAccount.setDescription(getString(R.string.integrated_inbox_title));
		integratedInboxAccount.setEmail(getString(R.string.integrated_inbox_detail));

		super.onCreate(icicle);

		Account[] accounts = Preferences.getPreferences(this).getAccounts();
		Intent intent = getIntent();
		boolean startup = intent.getBooleanExtra(EXTRA_STARTUP, true);
		if (accounts.length == 1)
		{
			onOpenAccount(accounts[0]);
			finish();
		}
		
		else
		{
			requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
			requestWindowFeature(Window.FEATURE_PROGRESS);

			setContentView(R.layout.encryption_basics);
			ListView listView = getListView();
			listView.setOnItemClickListener(this);
			listView.setItemsCanFocus(false);
			listView.setEmptyView(findViewById(R.id.empty));
			findViewById(R.id.next).setOnClickListener(this);
			registerForContextMenu(listView);

		}
	}


	@Override
	public void onPause()
	{
		super.onPause();
	}



	public void onClick(View view)
	{
		if (view.getId() == R.id.next)
		{
			onAddNewAccount();
		}
	}

	private void onAddNewAccount()
	{
		AccountSetupBasics.actionNewAccount(this);
		finish();
	}



	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}
	
	private void onOpenAccount(BaseAccount account)
	{
		if (account instanceof SearchAccount)
		{
			SearchAccount searchAccount = (SearchAccount)account;
			MessageList.actionHandle(this, searchAccount.getDescription(), searchAccount);
		}
		else
		{
			Account realAccount = (Account)account;
			if (Secure9.FOLDER_NONE.equals(realAccount.getAutoExpandFolderName()))
			{
				FolderList.actionHandleAccount(this, realAccount);
			}
			else
			{
				MessageList.actionHandleFolder(this, realAccount, realAccount.getAutoExpandFolderName());
			}
		}
	}
	
	private void refresh()
	{

		BaseAccount[] accounts = Preferences.getPreferences(this).getAccounts();

		List<BaseAccount> newAccounts = new ArrayList<BaseAccount>(accounts.length + 4);
		if (accounts.length > 0)
		{
			newAccounts.add(integratedInboxAccount);
			newAccounts.add(unreadAccount);
		}

		for (BaseAccount account : accounts)
		{
			newAccounts.add(account);
		}

	//	mAdapter = new AccountsAdapter(newAccounts.toArray(new BaseAccount[0]));
		getListView().setAdapter(mAdapter);
		if (newAccounts.size() > 0)
		{
		//	mHandler.progress(Window.PROGRESS_START);
		}
		pendingWork.clear();

		for (BaseAccount account : newAccounts)
		{

			if (account instanceof Account)
			{
				pendingWork.put(account, "true");
				Account realAccount = (Account)account;
				MessagingController.getInstance(getApplication()).getAccountStats(EncryptionBasics.this, realAccount, mListener);
			}
			else if (Secure9.countSearchMessages() && account instanceof SearchAccount)
			{
				pendingWork.put(account, "true");
				final SearchAccount searchAccount = (SearchAccount)account;

				MessagingController.getInstance(getApplication()).searchLocalMessages(searchAccount, null, new MessagingListener()
				{
					@Override
					public void searchStats(AccountStats stats)
					{
					mListener.accountStatusChanged(searchAccount, stats);
					}
				});
			}
		}

	}
	
	@Override
	public void onResume()
	{
		super.onResume();

		refresh();
		MessagingController.getInstance(getApplication()).addListener(mListener);
	}
}
